package com.kinyx.framework.image;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import com.kinyx.framework.annotation.assist.CanNotNull;
import com.kinyx.framework.annotation.assist.ReturnNotNull;
import com.kinyx.framework.annotation.assist.Tester;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * 图片绘画工具类
 */
@Tester("com.kinyx.framework.image.KImageDrawer_Tester_")
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class KImageDrawer {

	/**
	 * 添加行
	 * @param image 图片对象
	 * @param color 颜色
	 * @param y 起始Y坐标（负值表示最大值）
	 * @param height 行高
	 * @return 新图像
	 */
	@ReturnNotNull
	public static BufferedImage appendRow(@CanNotNull final BufferedImage image, @CanNotNull final Color color, @CanNotNull int y, @CanNotNull final int height) {
		final int biWidth = image.getWidth();
		final int biHeight = image.getHeight();
		y = (y < 0) ? biHeight : y;// 负值纠正
		final int biNewHeight = biHeight + height + (y > biHeight ? (y - biHeight) : 0);
		final BufferedImage biNew = new BufferedImage(biWidth, biNewHeight, image.getType());
		final Graphics g = biNew.getGraphics();
		g.setColor(color);
		g.fillRect(0, y, biWidth, height);
		if ((y == -1) || (y >= biHeight)) {
			g.drawImage(image, 0, 0, null);
		} else if (y == 0) {
			g.drawImage(image, 0, height, null);
		} else {
			g.drawImage(image, 0, 0, biWidth, y, 0, 0, biWidth, y, null);
			g.drawImage(image, 0, y + height, biWidth, biNewHeight, 0, y, biWidth, biHeight, null);
		}
		return biNew;
	}

}
